import java.sql.*;
import java.util.Scanner;
/*
JDBC连接Mysql
 */
public class demo {
    private static final String URL = "jdbc:mysql://localhost:3306/jdbcDemo";
    private static final String USERNAME = "root";
    private static final String PWD = "123456";

    //进行增删改
    private static void update(){
        Connection connection =null;
        Statement stmt = null;
        try {
//            1.导入驱动，加载具体的驱动类
            Class.forName("com.mysql.jdbc.Driver");
//            2.与数据库建立连接
            connection = DriverManager.getConnection(URL,USERNAME,PWD);
//            3.发送SQL语句并执行
            stmt = connection.createStatement();
            String sql = "insert into `13` value ('1','zs')";
//            执行SQL语句
            int count = stmt.executeUpdate(sql); //返回值表示更改了几条数据
//            处理结果
            if (count>0){
                System.out.println("操作成功！");
            }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try{
                if (stmt!=null) stmt.close();
                if (connection!=null) connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    //进行查询
    public static void query(){
        Connection connection =null;
        Statement stmt = null;
        ResultSet rs= null;
        try {
//            1.导入驱动，加载具体的驱动类
            Class.forName("com.mysql.jdbc.Driver");
//            2.与数据库建立连接
            connection = DriverManager.getConnection(URL,USERNAME,PWD);
//            3.发送SQL语句并执行
            stmt = connection.createStatement();
            Scanner input= new Scanner(System.in);
            System.out.println("输入id：");
            String id= input.nextLine();
            System.out.println("输入名字：");
            String name= input.nextLine();
            String sql= "SELECT * from jdbcDemo ";
//            执行SQL语句
            rs = stmt.executeQuery(sql); //返回值返回了一个集合
//            处理结果
            int count = -1;
            if(rs.next()) {
                count = rs.getInt(1) ;
            }
            if(count>0) {
                System.out.println("登陆成功！");
            }else {
                System.out.println("登陆失败！");
            }
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }catch (SQLException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try{
                //先打开的最后关
                if (rs!=null) rs.close();
                if (stmt!=null) stmt.close();
                if (connection!=null) connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args){
        update() ;
    }
}
